package wiki.hitime.stl.daq.manager;

import wiki.hitime.stl.daq.bean.db.DaqDbDetailConfig;
import wiki.hitime.stl.daq.bean.dto.manage.DbDetailConfigDTO;
import wiki.hitime.stl.daq.mapper.DaqDbDetailConfigMapper;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangdaoping
 * @create 2019-07-17 10:02
 */
@Service
public class DaqDbDetailConfigManager {

    @Resource
    private DaqDbDetailConfigMapper daqDbDetailConfigMapper;

    /**
     * 加载数据源详细配置
     *
     * @param dbConfigId 数据源配置编号
     * @return 数据源详细配置集合
     */
    public List<DaqDbDetailConfig> loadDbDetailConfig(Long dbConfigId) {
        Example selectDetailConfigExample = new Example(DaqDbDetailConfig.class, true, true);
        selectDetailConfigExample.createCriteria().andEqualTo("dbConfigId", dbConfigId).andEqualTo("isDelete", Boolean.FALSE);
        return daqDbDetailConfigMapper.selectByExample(selectDetailConfigExample);
    }

    /**
     * 批量添加数据库详细配置
     *
     * @param dtoList 数据库详细配置集合
     */
    public void addDbDetailConfig(List<DbDetailConfigDTO> dtoList) {
        List<DaqDbDetailConfig> dbDetailConfigList = dtoList.stream().map(this::convertDb).collect(Collectors.toList());
        daqDbDetailConfigMapper.insertList(dbDetailConfigList);
    }

    private DaqDbDetailConfig convertDb(DbDetailConfigDTO dto) {
        return DaqDbDetailConfig.builder()
                .dbConfigId(dto.getDbConfigId())
                .detailConfigName(dto.getDetailConfigName())
                .detailConfigKey(dto.getDetailConfigKey())
                .detailConfigValue(dto.getDetailConfigValue())
                .remark(dto.getRemark())
                .isDelete(Boolean.FALSE)
                .build();
    }
}
